
module iTimer.ui.ToolsBar ;
import iTimer.Types ;



final class AppToolsBar {
	
	static const char[] IMGS	= "TOOLSBAR";
	
	static ToolBar Bar;	
	static ToolItem Conf, Play, Pause, Stop, Refresh ;
	
    static const top_left_width = 164 ;
    
	static void load(ref Composite parent) {
        auto _parent_layout = new FormLayout () ;
        parent.setLayout ( _parent_layout );
        
        Label label1 = new Label(parent, DWT.HORIZONTAL|DWT.SEPARATOR);
        FormData data1 = new FormData();
        with( data1 ) {
            left    = new FormAttachment(0, 0);
            right   =  new FormAttachment(100, 0);
            top    = new FormAttachment(0, 0);
            height  = 2  ;
        }
        label1.setLayoutData(data1);
        // label1.setText("label1");
        label1.setBackground(GShell.getDisplay().getSystemColor(DWT.COLOR_WIDGET_BACKGROUND));
        
        Composite  top_right_com   = new Composite(parent,  DWT.NONE) ;
        FormData data3 = new FormData();
        with( data3 ) {
            left    = new FormAttachment(0, top_left_width );
            right   =  new FormAttachment(100, -4);
            top    = new FormAttachment(0, 4);
            height  = 24 ;
        }
        top_right_com.setLayoutData(data3);
        load_top_right(top_right_com);
        
		Bar = new ToolBar(parent, DWT.FLAT);
        FormData data2 = new FormData();
        with( data2 ) {
            left    = new FormAttachment(0, 0);
            right   =  new FormAttachment(0, top_left_width );
            top    = new FormAttachment(0, 4);
        }
        Bar.setLayoutData(data2);
        
		// loadConf();
		
       Play   =  new ToolItem( AppToolsBar.Bar, DWT.NONE );
       Play.setText("播放") ;
       Play.addListener (DWT.Selection, new class() Listener {
			void handleEvent (Event event) {
                iTaskList.play_task();
            }
        });  
        Play.setEnabled(false);
        
       Pause   =  new ToolItem( AppToolsBar.Bar, DWT.NONE );
       Pause.setText("暂停") ; 
       Pause.addListener (DWT.Selection, new class() Listener {
            void handleEvent (Event event) {
                iTaskList.pause_task();
            }
        });
        Pause.setEnabled(false);
        
        Stop   =  new ToolItem( AppToolsBar.Bar, DWT.NONE );
        Stop.setText("停止") ;
        Stop.addListener (DWT.Selection, new class() Listener {
            void handleEvent (Event event) {
                iTaskList.stop_task();
            }
        });
        Stop.setEnabled(false);
        
        Refresh   =  new ToolItem( AppToolsBar.Bar, DWT.NONE );
        Refresh.setText("刷新") ;
        Refresh.addListener (DWT.Selection, new class() Listener {
            void handleEvent (Event event) {
                iTaskList.list(-1, true, false) ;
            }
        });
        Refresh.setEnabled(true);
	}
    
	static void loadConf(){
		Conf = new ToolItem( AppToolsBar.Bar, DWT.DROP_DOWN);
		//Conf.setImage(DWTResourceManager.getImage("CONF", IMGS));
		//Conf.setDisabledImage(DWTResourceManager.getImage("CONF_DIS", IMGS));
		Conf.setText("任务");
		Conf.setToolTipText("配置任务");
		
		Conf.addListener (DWT.Selection, new class() Listener {
			void handleEvent (Event event) {
				if (event.detail == DWT.ARROW) {
                        assert(AppMenus.Task_Menu !is null );
                        assert(AppMenus.Bar !is null );
                        auto rect = AppToolsBar.Conf.getBounds ();
                        Point pt = new Point (rect.x, rect.y + rect.height);
                        pt = AppToolsBar.Bar.toDisplay (pt);
                        AppMenus.Task_Menu.setLocation (pt.x, pt.y);
                        AppMenus.Task_Menu.setVisible (true);
				}else{
					// AppConf.OpenLast();
				}
			}
		});
        
	}
    
    static Label        _current_pos  ;
    static Color[2]   _pos_colors ;
    static Atomic!(int)  _pos_current ;
    
    
    static void pos_enable( bool is_enabled = false,  int current = 0 , int total = 0, bool force_reload = false ){
        
        if( is_enabled ) {
             _current_pos.setForeground( _pos_colors[0] );
        } else {
             _current_pos.setForeground( _pos_colors[1] );
        }
        
        static void setTime(vBuffer bu, int sec ) {
            auto minute = sec / 60 ;
            if( minute <= 9 ) {
                bu('0');
            } 
            bu(  minute) ;
            bu(':');
            auto _sec    = sec % 60 ;
            if( _sec <= 9 ) {
                bu('0');
            } 
            bu(  _sec) ;
        }
        
        /*
            if( vPlayer.update_counter % 8 is 0 || force_reload ){
                version(DEV)  log!(__FILE__, __LINE__)("pos_enable current={} , cache={}",  current, total, _pos_current.load());
            }
            */
        
        if( force_reload || _pos_current.load() !is current ) {
            _pos_current.store(current);
            scope ubyte[] tmp = new ubyte[512] ;
            scope bu    = new vBuffer(tmp);
            setTime(bu, current);
            bu(" / ") ;
            setTime(bu, total);
            auto text    = cast(char[]) bu.slice   ;
             _current_pos.setText(  cast(char[]) text ) ;
        }

    }
    
    static void load_top_right(Composite parent){
        
        _pos_current.store( 0xfffff ) ;
        
        
		_pos_colors[0]   =  new Color( GShell.getDisplay().getDevice(), 0x99, 0x66, 0x00);
        _pos_colors[1]   = new Color( GShell.getDisplay().getDevice(), 0xaa, 0xaa, 0xaa);
        
        // parent.setBackground(GShell.getDisplay().getSystemColor(DWT.COLOR_RED));
        auto _parent_layout = new FormLayout();
        parent.setLayout(_parent_layout);
        
        _current_pos    = new Label(parent, DWT.PUSH) ;
       FormData data1 = new FormData();
        with( data1) {
            left    = new FormAttachment(0, 0 );
            right   =  new FormAttachment(0, 160);
            top    = new FormAttachment(0, 5);
        }
        _current_pos.setLayoutData(data1);
        _current_pos.setBackground(GShell.getDisplay().getSystemColor(DWT.COLOR_WIDGET_BACKGROUND));
        pos_enable();
    }
}